const { update, remove, add, getCount, query, getAll } = require('../../utils/dbTools');
const { getUploadedUrl } = require('../../utils/upload');
const tableName = 'staff';
// const departmentTableName = 'department';
// const positionTableName = 'position';

/**
 * 获取数据总条数
 * 
*/

const staffTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { bus_id } = req.query;
    const map = {}
    if (bus_id) map.bus_id = bus_id
    const result = await getCount(tableName, map);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}
/**
 * 获取员工列表
 * 
*/
const staffList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size, bus_id } = req.query;
    const map = {};
    if (page && size) {
        map.page = page;
        map.size = size;
    }
    if (bus_id) map.bus_id = bus_id;
    const result = await getAll(tableName, map);
    // let sql = `select s.id,s.avatar,s.name,s.gender,s.birthday,s.avatar,s.phone,s.status,s.create_time,d.id as departmentid,d.name as department,p.id as positionid, p.name as position from ${tableName} s inner join ${departmentTableName} d on s.departmentid=d.id inner join ${positionTableName} p on s.positionid=p.id limit ${(page - 1) * size},${size} `;
    // const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    result.forEach(item => {
        if (item.tags) item.tags = item.tags.split(',')
    })
    output.data = result;
    res.send(output);
}

/**
 * 新增员工信息
 * 
*/

const staffAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { departmentid, positionid, name, phone, gender, birthday, status } = req.body;
    console.log(req.body, 'req.body');
    if (!departmentid || !positionid || !name || !phone || !gender || !birthday || !status) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    birthday = new Date(birthday).getTime();
    const data = {
        departmentid, positionid, name, phone, gender, birthday, status, create_time: Date.now()
    }
    // 获取上传图片的地址
    if (req.file) data.avatar = getUploadedUrl(req.file);

    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    res.send(output);
}

/**
 *  删除员工信息
 * 
*/

const staffRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }

    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 更新员工信息: 仅支持更新状态
*/
const staffUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, departmentid, positionid, name, phone, gender, birthday, status } = req.body;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    birthday = new Date(birthday).getTime();
    const data = { departmentid, positionid, name, phone, gender, birthday, status }
    // 获取上传图片的地址
    if (req.file) data.avatar = getUploadedUrl(req.file);

    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}


module.exports = {
    staffAdd,
    staffRemove,
    staffUpdate,
    staffList,
    staffTotal
}